package com.gongzonqiang.controller.client;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.gongzonqiang.pojo.Collection;
import com.gongzonqiang.pojo.Comment;
import com.gongzonqiang.pojo.Notice;
import com.gongzonqiang.pojo.User;
import com.gongzonqiang.povo.NoticeVo;
import com.gongzonqiang.service.NoticeService;
import com.gongzonqiang.utils.ResResult;
import com.gongzonqiang.utils.UserRedisUtil;

/**
 * 公告controller
 * @author youjie
 *
 */
@RestController
@RequestMapping("/client/notice")
public class NoticeController {
	
	@Autowired
	UserRedisUtil userRedisUtil;
	@Autowired
	private NoticeService noticeService;
	
	/**
	 * 	获取轮播图公告
	 * @return
	 */
	@RequestMapping("/listNoticeForIndex")
	public List<Notice> listNoticeForIndex() {
		return noticeService.listNoticeForIndex();
	}
	
	/**
	 * 首页发布的公告
	 * @param pageNum
	 * @param pageSize
	 * @param order 0置顶优先,其次时间  1置顶优先其次浏览数
	 * @return
	 */
	@RequestMapping("/indexNotices")
	public ResResult indexNotices(Integer pageNum, Integer pageSize,Integer order) {
		PageInfo<NoticeVo> noticeVos = noticeService.selectNoticeVo(pageNum, pageSize, null, order, false);
		return ResResult.ok(noticeVos);
	}
	
	/**
	 *  查看分类下的商品
	 * @param pageNum
	 * @param pageSize
	 * @param order 0置顶优先,其次时间  1置顶优先其次浏览数
	 * @param categoryId
	 * @return 公告信息Vo(置顶和非置顶;包含公告信息,发布公告用户基本信息(昵称,头像,电话);分页)
	 */
	@RequestMapping("/listNoticeByCategory")
	public ResResult listNoticeByCategory(int pageNum, int pageSize,int order, Long categoryId) {
		PageInfo<NoticeVo> notices = noticeService.listNoticeByCategory(pageNum, pageSize, order,categoryId);
		return ResResult.ok(notices);
	}
	
	/**
	 *	 根据notice中的content，id,categoryId搜索公告
	 * @param pageNum
	 * @param pageSize
	 * @param notice
	 * @return
	 */
	@RequestMapping("/searchNotice")
	public ResResult searchNotice(int pageNum, int pageSize, NoticeVo notice) {
		PageInfo<NoticeVo> searchNotice = noticeService.searchNotice(pageNum, pageSize, notice);
		return ResResult.ok(searchNotice);
	}
	
	/**
	 * 根据公告id获取公告详细
	 * @param id
	 * @return 公告基本信息，谁发布的，评论详细。
	 */
	@RequestMapping("/noticeDesc/{id}")
	public ResResult noticeDesc(@PathVariable Long id) {
		NoticeVo noticeDesc = noticeService.noticeDesc(id);
		return ResResult.ok(noticeDesc);
	}
	
	/**
	 * 	用户发布公告
	 * @param notice
	 * @param token
	 * @return
	 */
	@RequestMapping("/release")
	public ResResult release(Notice notice, String token) {
		// TODO 如果指定支付应该返回支付信息
		
		User loginUser = userRedisUtil.getUser(token);
		int release = noticeService.release(notice, loginUser);
		return ResResult.ok(release); // 1 成功 0失败
	}
	
	/**
	 *	 用户删除自己发布的公告
	 * 
	 * @param noticeId 公告id
	 * @param user     用户实体对象
	 * @return 0失败 1成功
	 */
	@RequestMapping("/delNotice")
	public ResResult delNotice(Long noticeId,String token) {
		User loginUser = userRedisUtil.getUser(token);
		if(noticeId==null)
			return ResResult.build(-1, "");
		int delNotice = noticeService.delNotice(noticeId, loginUser);
		return ResResult.ok(delNotice);
	}
	/**
	 * 用户删除自己的收藏
	 * 
	 * @param collectionId 收藏编号
	 * @param user         用户实体对象
	 * @return 0失败 1成功
	 */
	@RequestMapping("/delCollection")
	public ResResult delCollection(Long collectionId,String token) {
		User loginUser = userRedisUtil.getUser(token);
		if(collectionId==null)
			return ResResult.build(-1, "");
		int delNotice = noticeService.delCollection(collectionId, loginUser);
		return ResResult.ok(delNotice);
	}
	
	/**
	 * 	用户删除自己的评论
	 * 
	 * @param commentId 评论编号
	 * @param user      用户实体
	 * @return 0失败 1成功
	 */
	@RequestMapping("/delComment")
	public ResResult delComment(Long commentId, String token) {
		User loginUser = userRedisUtil.getUser(token);
		if(commentId==null)
			return ResResult.build(-1, "");
		int delNotice = noticeService.delComment(commentId, loginUser);
		return ResResult.ok(delNotice);
	}
	
	/**
	 * 评论公告(同时要创建一条消息通知)
	 * @param comment 评论实体对象
	 * @param user 用户实体对象
	 * @return 0失败 1成功
	 */
	@RequestMapping("/comment")
	public ResResult comment(Comment comment,String token) {
		User user = userRedisUtil.getUser(token);
		noticeService.comment(comment, user);
		
		return ResResult.ok();
	}
	
	/**
	 * 用户收藏公告
	 * 
	 * @param noticeId 公告id
	 * @param user     用户实体对象
	 * @return 0失败 1成功
	 */
	@RequestMapping("/collect")
	public ResResult collect(Long noticeId,String token) {
		User user = userRedisUtil.getUser(token);
		
		return ResResult.ok(noticeService.collect(noticeId, user));
	}
	
	/**
	 *判断用户是否收藏 
	 * @param noticeId 公告编号
	 * @param user 用户实体
	 * @return 
	 */
	@RequestMapping("/isCollect")
	public Collection isCollect(Long noticeId,String token) {
		User user = userRedisUtil.getUser(token);
		
		return noticeService.isCollect(noticeId,user);
	}
}
